package cn.harvest.storage.bo;

import java.util.List;

import cn.harvest.storage.bo.admin.OperatorService;
import cn.harvest.storage.dao.DAO;
import cn.harvest.storage.po.Employee;
import cn.harvest.storage.vo.EmployeeVo;

public class LoginService {

	private DAO dao = DAO.getDAO();
	
	private LoginService() {

	}

	public static LoginService getService() {

		LoginService service = new LoginService();
		return service;
	}
	/**
	 * 根据用户名密码查询用户是否存在
	 * @param vo
	 * @return
	 */
	public EmployeeVo getOperator(Employee emp) {
		
		StringBuffer sql = new StringBuffer();
		sql.append(" SELECT em.employee_id, em.employee_cid, em.employment_id, em.employee_name,");
		sql.append("        em.employee_state, em.employee_tel, em.employee_add, em.employee_memo,"); 
		sql.append("        em.isOper, em.operator_name, em.operator_pass, eem.employment_name");
		sql.append(" FROM employee em, employment eem ");
		sql.append(" WHERE em.employment_id = eem.employment_id");
		sql.append(" AND em.operator_name = '"+ emp.getOperatorName() + "'");
		sql.append(" AND em.operator_pass='" + emp.getOperatorPass() + "'");

		System.out.println("### - LoginService -> getOperator -> #SQL \r\n	 " + sql);
		
		List list = dao.selectBySQL(sql.toString());

		// obj[0]em.employee_id, obj[1]em.employee_cid, obj[2]em.employment_id,
		// obj[3]em.employee_name, obj[4]em.employee_state, obj[5]em.employee_tel,
		// obj[6]em.employee_add, obj[7]em.employee_memo, obj[8]em.isOper, 
		// obj[9]em.operator_name, obj[10]em.operator_pass
		EmployeeVo operator = null;
		if (list.size() > 0) {
			Object[] obj = (Object[]) list.get(0);
			operator = new EmployeeVo(obj[0].toString(), obj[1].toString(), obj[2].toString(), obj[3].toString(), obj[4].toString(), obj[5].toString(), obj[6].toString(), obj[7].toString(), obj[8].toString(), obj[9].toString(), obj[10].toString(), obj[11].toString());
			OperatorService operatorService = OperatorService.getService();
			List operatorPermissionList = operatorService.selectOperatorInfo(obj[0].toString());
			operator.setOperatorPermissionList(operatorPermissionList);
		}

		return operator;
	}
}
